<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <%@include file="../../../includes/common.jsp" %>
    <title><spring:message code="dictDefine"/></title>
</head>
<body>
<div id="mainContent" class="easyui-panel" data-options="fit:true" style="padding: 10px">
    <form class="form-inline">
        课件名称：
        <input type="text" class="input-medium"
               name="courseName" id="courseName" value=""/>
        章节总数：
        <input type="text" class="input-medium"
               name="chapterNumber" id="chapterNumber" value=""/>
        <a class="btn btn-primary" onclick="query()"><spring:message code="query"/></a>
        <a class="btn btn-primary" onclick="audit()"><spring:message code="audit"/></a>
        <a class="btn btn-primary" onclick="audit()">驳回</a>
        <a class="btn btn-primary" onclick="addDictDefine()"><spring:message code="add"/></a>
    </form>
    <table id="dataTable"></table>
    <div id="dataEdit" closed="true" modal="true" style="overflow: hidden">
        <iframe style="width: 100%;height: 100%"
                id="contentList"
                name="contentList"
                marginheight="0"
                marginwidth="0"
                frameborder="0" src=""
                allowTransparency="true">
        </iframe>
    </div>
</div>
</body>
</html>
<script type="text/javascript">
    function query() {
        listOption.url = basePath + "course/listAllCourse";
        listOption.data = "courseName=" + $("#courseName").val().trim() + "&chapterNum=" + $("#chapterNumber").val().trim();
        listOption.pageNumber = 1;
        loadData(listOption);
    }

    function initDataGrid() {
        $('#dataTable').datagrid({
            title: "课件列表",
            pagination: true,
            fitColumns: true,
            columns: [
                [
                    {field: 'id', title: "&nbsp<input id='checkAll' type='checkbox' onclick='selectAll()'/>", align: 'center', width: 100},
                    {field: 'courseName', title: '课件名称', align: 'center', width: 200},
                    {field: 'chapterNum', title: '章节总数', sortable: true, align: 'center', width: 200},
                    {field: 'showCourseType', title: '课件类型', align: 'center', width: 200},
                    {field: 'showState', title: '课件状态', align: 'center', width: 200},
                    {field: 'upUserName', title: '上传人', align: 'center', width: 200},
                    {field: 'operate', title: '<spring:message code="operate"/>', align: 'center', width: 200 }
                ]
            ],
            sortName: "",
            sortOrder: "asc",
            onSortColumn: onSortColumn
        });
    }

    function onSortColumn(sortColumn, sortDirection) {
        switch (sortColumn) {
            case "url":
                sortColumn = "url";
                break;
        }
        onSortColumnDefault(sortColumn, sortDirection);
    }

    function format(data) {
        data.resultList = queryFormat(data.resultList);
        return data;
    }

    function queryFormat(temp) {
        for (var i in temp) {
            temp[i].operate = "<a onclick='editDictDefine(\"" + temp[i].id + "\")'>编辑</a>&nbsp;&nbsp;"
                    + "<a onclick='delDictDefine(\"" + temp[i].id + "\")'>删除</a>&nbsp;&nbsp;";
            temp[i].courseName = "<a onclick='watchCourse(\"" + temp[i].id + "\")'>" + temp[i].courseName + "</a>"
            temp[i].id = "&nbsp;<input type='checkbox' name='checkBox' id='" + temp[i].id + "' value='" + temp[i].id + "'\">&nbsp;";
        }
        return temp;
    }

    function addDictDefine() {
        var path = basePath + "course/addCourse";
        $("#contentList").attr("src", path);
        $('#dataEdit').dialog({
            title: '<spring:message code="add"/>',
            height: 400,
            width: 600
        }).dialog('open');
    }

    function editDictDefine(id) {
        var path = basePath + "course/editCourseInfoDo?id=" + id;
        $("#contentList").attr("src", path);
        $('#dataEdit').dialog({
            title: '<spring:message code="edit"/>',
            height: 300,
            width: 600
        }).dialog('open');
    }

    function delDictDefine(id) {
        $.messager.confirm("提交", "确认提交？", function (r) {
            if (r) {
                $.ajax({
                    url: basePath + "course/deleteCourseInfo?id=" + id,
                    dataType: "json",
                    type: "DELETE",
                    success: function () {
                        $.messager.alert("成功", "删除成功！", "", function () {
                            query();
                        })
                    },
                    error: doError
                })
            }
        })
    }

    function audit() {
        var ids = "";
        var value;
        var flag = 0;
        var allBox = $("[name='checkBox']");
        for (var i = 0; i < allBox.length; i++) {
            value = allBox[i].getAttribute("value");
            if ($("#" + value).attr("checked") == "checked") {
                flag = 1;
                ids += value + ",";
            }
        }
        if (flag == 0) {
            $.messager.alert("提交", "请选择审核项");
            return;
        }
        $.messager.confirm("提交", "确认提交？", function (r) {
                    if (r) {
                        $.ajax({
                            url: basePath + "course/checkAuditInfoSubmitState?ids=" + ids,
                            dataType: "json",
                            type: "get",
                            success: function (isAudit) {
                                if (isAudit == false) {
                                    $.messager.alert("提交", "只有未审核的才能提交！");
                                } else {
                                    $.ajax({
                                        url: basePath + "course/changeAuditStateToAudited?ids=" + ids,
                                        dataType: "json",
                                        type: "get",
                                        success: function () {
                                            $.messager.alert("成功", "审核成功！", "", function () {
                                                query();
                                            })
                                        },
                                        error: doError
                                    })
                                }
                            },
                            error: doError
                        })
                    }
                }
        )
    }

    function selectAll() {
        if ($("#checkAll").attr("checked") == "checked") {
            $("[name='checkBox']").attr("checked", true);
        } else {
            $("[name='checkBox']").attr("checked", false);
        }
    }

    function watchCourse(id) {
        location.href = basePath + "course/adminWatchCourse?id=" + id;
    }

    $(function () {
        initDataGrid();
        query();
    })
</script>